package com.gewu.pm.ui.activity.payment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gewu.pm.R;
import com.gewu.pm.aop.SingleClick;
import com.gewu.pm.app.AppActivity;
import com.gewu.pm.bean.TestBean;
import com.gewu.pm.ui.adapter.PaymentTermAdapter;
import com.hjq.bar.TitleBar;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 账期单列表
 */
public final class PaymentTermActivity extends AppActivity 
        implements OnRefreshLoadMoreListener {

    private TitleBar mTitleBar;
    private EditText mSearchEdit;
    private SmartRefreshLayout mRefreshLayout;
    private RecyclerView mRecyclerView;
    private PaymentTermAdapter mAdapter;

    private String mSearchKeyword = "";
    private int mCurrentPage = 1;
    private static final int PAGE_SIZE = 10;

    public static void start(Context context) {
        Intent intent = new Intent(context, PaymentTermActivity.class);
        context.startActivity(intent);
    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_payment_term;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mTitleBar = findViewById(R.id.tb_title);
        mSearchEdit = findViewById(R.id.et_search);
        mRefreshLayout = findViewById(R.id.srl_refresh);
        mRecyclerView = findViewById(R.id.rv_list);

        // 设置标题栏
        mTitleBar.setOnTitleBarListener(this);

        // 设置搜索框提示
        mSearchEdit.setHint("输入终端名称/编码/单据编号");

        // 设置搜索监听
        mSearchEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                mSearchKeyword = s.toString().trim();
                // 搜索时重置页码
                mCurrentPage = 1;
                loadData();
            }
        });

        // 设置下拉刷新
        mRefreshLayout.setOnRefreshLoadMoreListener(this);

        // 设置列表
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new PaymentTermAdapter(this);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        loadData();
    }

    /**
     * 加载数据
     */
    private void loadData() {
        // 模拟数据加载
        postDelayed(() -> {
            List<TestBean> dataList = new ArrayList<>();

            // 第一页添加3条数据
            if (mCurrentPage == 1) {
                dataList.add(new TestBean("DJ19292345454492", "落落超市"));
                dataList.add(new TestBean("DJ19292345454493", "乐乐超市"));
                dataList.add(new TestBean("DJ19292345454494", "哈哈超市"));
                mAdapter.setData(dataList);
            } else {
                // 模拟分页数据（每页10条）
                for (int i = 0; i < PAGE_SIZE; i++) {
                    int index = (mCurrentPage - 1) * PAGE_SIZE + i;
                    dataList.add(new TestBean("DJ" + index, "超市" + index));
                }
                mAdapter.addData(dataList);
            }

            // 完成刷新
            mRefreshLayout.finishRefresh();
            mRefreshLayout.finishLoadMore();

            // 模拟最后一页（第5页）
            if (mCurrentPage >= 5) {
                mRefreshLayout.setNoMoreData(true);
            }
        }, 100);
    }

    @SingleClick
    @Override
    public void onRightClick(View view) {
        // 点击筛选按钮
        startActivity(new Intent(this, PaymentTermPostponeActivity.class));
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        // 下拉刷新
        mCurrentPage = 1;
        mRefreshLayout.setNoMoreData(false);
        loadData();
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        // 上拉加载更多
        mCurrentPage++;
        loadData();
    }
}

